/*
 * utf8stringobject.cpp
 *
 *  Created on: Oct 27, 2009
 *      Author: Silviu Caragea
 */

#include "includes/utf8stringobject.h"

namespace templatipus
{

template<typename T> void TUtf8StringObject::internalNumberToString(std::string & buffer, utf8Ptr strFormat, const T value)
{
	std::ostringstream ss;
	ss << value;
	buffer = ss.str();
}

//constructors

TUtf8StringObject::TUtf8StringObject():TStringObject()
{

}

TUtf8StringObject::TUtf8StringObject(const TemplateObject &other):TStringObject(other)
{
	if (this != &other)
		this->internalBuffer = (std::string) other;
}

TUtf8StringObject::TUtf8StringObject(const tbool &rhs):TStringObject(rhs)
{
	TUtf8StringObject::internalNumberToString(internalBuffer, "%d", rhs);
}

TUtf8StringObject::TUtf8StringObject(const int8 &rhs):TStringObject(rhs)
{
	TUtf8StringObject::internalNumberToString(internalBuffer, "%d", rhs);
}

TUtf8StringObject::TUtf8StringObject(const int16 &rhs):TStringObject(rhs)
{
	TUtf8StringObject::internalNumberToString(internalBuffer, "%d", rhs);
}

TUtf8StringObject::TUtf8StringObject(const int32 &rhs):TStringObject(rhs)
{
	TUtf8StringObject::internalNumberToString(internalBuffer, "%d", rhs);
}

TUtf8StringObject::TUtf8StringObject(const int64 &rhs):TStringObject(rhs)
{
	TUtf8StringObject::internalNumberToString(internalBuffer,
			PRINTF_INT64_MODIFIER, rhs);
}

TUtf8StringObject::TUtf8StringObject(const float32 &rhs):TStringObject(rhs)
{
	TUtf8StringObject::internalNumberToString(internalBuffer, "%0.2f", rhs);
}

TUtf8StringObject::TUtf8StringObject(const float64 &rhs):TStringObject(rhs)
{
	TUtf8StringObject::internalNumberToString(internalBuffer, "%0.2f", rhs);
}

TUtf8StringObject::TUtf8StringObject(const std::wstring& rhs):TStringObject(rhs)
{
	this->internalBuffer = internalBuffer.assign(rhs.begin(), rhs.end());
}

TUtf8StringObject::TUtf8StringObject(const std::string& rhs):TStringObject(rhs)
{
	this->internalBuffer = rhs;
}

TUtf8StringObject::TUtf8StringObject(const utf8Ptr &rhs):TStringObject(rhs)
{
	this->internalBuffer = rhs;
}

TUtf8StringObject::TUtf8StringObject(const unicodePtr &rhs):TStringObject(rhs)
{
	std::wstring ws(rhs);
	this->internalBuffer = internalBuffer.assign(ws.begin(), ws.end());
}

TUtf8StringObject::~TUtf8StringObject() {

}

//base methods

TObjectType TUtf8StringObject::getType() const {
	return STRING_UTF8_TYPE;
}

//operations
TUtf8StringObject TUtf8StringObject::operator +(const TemplateObject &other) {
	TUtf8StringObject obj = internalBuffer;
	obj += other;

	return obj;
}

const TemplateObject& TUtf8StringObject::operator +=(
		const TemplateObject &other) {
	internalBuffer.append(((std::string) other).c_str());
	return *this;
}

//assignment

TemplateObject& TUtf8StringObject::operator=(const TemplateObject &other) {
	if (this != &other)
		this->internalBuffer = (std::string) other;

	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const int8 &other) {
	TUtf8StringObject::internalNumberToString(internalBuffer, "%d", other);
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const int16 &other) {
	TUtf8StringObject::internalNumberToString(internalBuffer, "%d", other);
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const int32 &other) {
	TUtf8StringObject::internalNumberToString(internalBuffer, "%d", other);
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const int64 &other) {
	TUtf8StringObject::internalNumberToString(internalBuffer,
			PRINTF_INT64_MODIFIER, other);
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const float32 &other) {
	TUtf8StringObject::internalNumberToString(internalBuffer, "%0.2f", other);
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const float64 &other) {
	TUtf8StringObject::internalNumberToString(internalBuffer, "%0.2f", other);
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const std::wstring& other) {
	this->internalBuffer.assign(other.begin(), other.end());
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const std::string& other) {
	internalBuffer = other;
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const unicodePtr other) {
	std::wstring wother = other;
	this->internalBuffer.assign(wother.begin(), wother.end());
	return *this;
}

TemplateObject& TUtf8StringObject::operator=(const utf8Ptr other) {
	internalBuffer = other;
	return *this;
}

//conversions to numbers
tbool TUtf8StringObject::toBool() const {
	int32 temp;
	sscanf(internalBuffer.c_str(), "%ld", &temp);
	return (tbool) temp;
}

int8 TUtf8StringObject::toInt8() const {
	int32 temp;
	sscanf(internalBuffer.c_str(), "%ld", &temp);
	return (int8) temp;
}

uint8 TUtf8StringObject::toUint8() const {
	int32 temp;
	sscanf(internalBuffer.c_str(), "%ld", &temp);
	return (uint8) temp;
}

int16 TUtf8StringObject::toInt16() const {
	int32 temp;
	sscanf(internalBuffer.c_str(), "%ld", &temp);
	return (int16) temp;
}

uint16 TUtf8StringObject::toUint16() const {
	int32 temp;
	sscanf(internalBuffer.c_str(), "%ld", &temp);
	return (uint16) temp;
}

int32 TUtf8StringObject::toInt32() const {
	int32 temp;
	sscanf(internalBuffer.c_str(), "%ld", &temp);
	return temp;
}

uint32 TUtf8StringObject::toUint32() const {
	uint32 temp;
	sscanf(internalBuffer.c_str(), "%lu", &temp);
	return temp;
}

int64 TUtf8StringObject::toInt64() const {
	int64 temp;
	sscanf(internalBuffer.c_str(), PRINTF_INT64_MODIFIER, &temp);
	return temp;
}

uint64 TUtf8StringObject::toUint64() const {
	uint64 temp;
	sscanf(internalBuffer.c_str(), PRINTF_INT64_MODIFIER, &temp);
	return temp;
}

float32 TUtf8StringObject::toFloat32() const {
	float32 temp;
	sscanf(internalBuffer.c_str(), "%f", &temp);
	return temp;
}

float64 TUtf8StringObject::toFloat64() const {
	float64 temp;
	sscanf(internalBuffer.c_str(), "%lf", &temp);
	return temp;
}

std::string TUtf8StringObject::toString() const {
	return this->internalBuffer;
}

std::wstring TUtf8StringObject::toWString() const {
	std::wstring ws;
	ws.assign(internalBuffer.begin(), internalBuffer.end());
	return ws;
}

//methods comparison

tbool TUtf8StringObject::equals(const TemplateObject &other) const {

	return (internalBuffer.compare(((std::string) other).c_str()) == 0);
}

tbool TUtf8StringObject::notEquals(const TemplateObject &other) const {
	return (internalBuffer.compare(((std::string) other).c_str()) != 0);
}

tbool TUtf8StringObject::greater(const TemplateObject &other) const {
	return (internalBuffer.compare(((std::string) other).c_str()) > 0);
}

tbool TUtf8StringObject::less(const TemplateObject &other) const {
	return (internalBuffer.compare(((std::string) other).c_str()) < 0);
}

tbool TUtf8StringObject::lessOrEquals(const TemplateObject &other) const {
	return (internalBuffer.compare(((std::string) other).c_str()) <= 0);
}

tbool TUtf8StringObject::greaterOrEquals(const TemplateObject &other) const {
	return (this->internalBuffer.compare(((std::string) other).c_str()) >= 0);
}

//stuff

utf8c TUtf8StringObject::operator [](const uint32 index) {
	return this->characterAtIndex(index);
}

uint32 TUtf8StringObject::getLength() {
	return (uint32) internalBuffer.size();

}

utf8c TUtf8StringObject::characterAtIndex(uint32 index) {
	if (index < internalBuffer.size())
		return internalBuffer.at(index);

	return NULL;
}

//append operations

void TUtf8StringObject::appendString(TStringObject &object) {
	const std::string & toApp = (std::string) object;
	internalBuffer.append(toApp.c_str());
}

void TUtf8StringObject::appendUint8(uint8 nr) {
	std::string buffer;
	TUtf8StringObject::internalNumberToString(buffer, "%d", nr);
	internalBuffer.append(buffer);
}

void TUtf8StringObject::appendInt16(int16 nr) {
	std::string buffer;
	TUtf8StringObject::internalNumberToString(buffer, "%d", nr);
	internalBuffer.append(buffer);
}

void TUtf8StringObject::appendUint16(uint16 nr) {
	std::string buffer;
	TUtf8StringObject::internalNumberToString(buffer, "%d", nr);
	internalBuffer.append(buffer);
}

void TUtf8StringObject::appendInt32(int32 nr) {
	std::string buffer;
	TUtf8StringObject::internalNumberToString(buffer, "%d", nr);
	internalBuffer.append(buffer);
}

void TUtf8StringObject::appendUint32(uint32 nr) {
	std::string buffer;
	TUtf8StringObject::internalNumberToString(buffer, "%d", nr);
	internalBuffer.append(buffer);
}

void TUtf8StringObject::appendInt64(int64 nr) {
	std::string buffer;
	TUtf8StringObject::internalNumberToString(buffer, "%d", nr);
	internalBuffer.append(buffer);
}

void TUtf8StringObject::appendUint64(uint64 nr) {
	std::string buffer;
	TUtf8StringObject::internalNumberToString(buffer, "%d", nr);
	internalBuffer.append(buffer);
}

void TUtf8StringObject::appendUtf8Ptr(utf8Ptr ptr) {
	internalBuffer.append(ptr);
}

void TUtf8StringObject::appendString(std::string &str) {
	internalBuffer.append(str);
}

}
